/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.gui.view.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import android.util.Log;
import android.view.View;
import android.widget.TextView;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.data.DataBaseConnector;
import de.fuberlin.kosmik.data.dao.TrackRecord;
import de.fuberlin.kosmik.gui.activity.TrackActivity;
import de.fuberlin.kosmik.gui.view.adapter.TrackListAdapter.TrackListItem;
import de.fuberlin.kosmik.gui.view.adapter.TrackListAdapter.ViewHolderHeader;
import de.fuberlin.kosmik.gui.view.adapter.TrackListAdapter.ViewHolderTrack;
import de.fuberlin.kosmik.log.Constants;

/**
 * The TrackListAdapter obtains a list of all tracks from the database and
 * provides the list for the list view in the StartActivity.
 * 
 * @author micha
 * 
 */
public class TrackListAdapter extends
		ListWithHeadersAdapter<TrackListItem, ViewHolderHeader, ViewHolderTrack> {

	private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(
			"dd.MM.yy HH:mm:ss");
	private final String datePrefix;
	private DataBaseConnector dbConnector;
	private int nrCounter;
	private TrackActivity trackActivity;

	/**
	 * Constructor.
	 * 
	 * @param context of the caller.
	 */
	public TrackListAdapter(TrackActivity context) {
		super(context, R.layout.track_list_header, R.layout.track_list_row);
		trackActivity = context;
		dbConnector = trackActivity.getDataBase();
		datePrefix = context.getString(R.string.track_list__track_date_added);
		nrCounter = 1;
	}

	/**
	 * Refreshes the list of tracks.
	 */
	public void refreshTracks() {
		this.clear();
		nrCounter = 1;
		addTracks();
	}

	private void addTracks() {

		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());

		long now = cal.getTimeInMillis();

		// roll to start of today
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);

		long startToday = cal.getTimeInMillis();

		// roll to yesterday
		cal.add(Calendar.DAY_OF_YEAR, -1);
		long startYesterday = cal.getTimeInMillis();

		// roll two weeks into past
		cal.add(Calendar.WEEK_OF_YEAR, -2);
		long startTwoWeeksAgo = cal.getTimeInMillis();

		// adds today's tracks
		addTracksWithHeader(startToday, now - 1, R.string.track_list__today);
		// adds yesterday's tracks
		addTracksWithHeader(startYesterday, startToday - 1, R.string.track_list__yesterday);
		// adds tracks of the last two weeks
		addTracksWithHeader(startTwoWeeksAgo, startYesterday - 1,
				R.string.track_list__last_two_weeks);
		// adds all tracks which are older than two weeks
		addTracksWithHeader(0, startTwoWeeksAgo - 1, R.string.track_list__long_time_ago);
	}

	private void addTracksWithHeader(long dateFrom, long dateTo, int stringRes) {
		// DataBaseConnector dbConnector = trackActivity.getDataBase();
		Collection<TrackRecord> trackCollection = dbConnector.getAllTracksInTimeWindow(
				dateFrom, dateTo);
		if (trackCollection.size() > 0) {
			Log.d(Constants.TAG, "Collectionsize: " + trackCollection.size());
			// add header to the array, the title of the header is in a dummy
			// TrackRecorcd object
			add(new TrackListItem(trackActivity.getString(stringRes)));
			for (TrackRecord trR : trackCollection) {
				add(new TrackListItem(trR, nrCounter++));
			}
		}
	}

	@Override
	ViewHolderHeader findViewsOfHeader(View convView) {
		ViewHolderHeader headerHolder = new ViewHolderHeader();
		headerHolder.headerName = (TextView) convView.findViewById(R.id.track_list_header_name);
		return headerHolder;
	}

	@Override
	void setViewsOfHeader(ViewHolderHeader headerHolder, TrackListItem listItem) {
		headerHolder.headerName.setText(listItem.headerName);
	}

	@Override
	ViewHolderTrack findViewsOfItem(View convView) {
		Log.d(Constants.TAG, "findViewsOfItem");
		ViewHolderTrack trackHolder = new ViewHolderTrack();
		trackHolder.trackName = (TextView) convView.findViewById(R.id.track_list_name);
		trackHolder.trackAddedTime = (TextView) convView.findViewById(R.id.track_list_date);
		return trackHolder;
	}

	@Override
	void setViewsOfItem(ViewHolderTrack trackHolder, TrackListItem trackListItem) {
		Log.d(Constants.TAG, "setViewsOfItem");
		trackHolder.trackName.setText(trackListItem.nr + ") "
				+ trackListItem.trackRecord.trackName);
		trackHolder.trackAddedTime.setText(String.format(datePrefix, SIMPLE_DATE_FORMAT
				.format(Long.valueOf(trackListItem.trackRecord.dateTrackAdded))));
		trackHolder.trackListItem = trackListItem;
	}

	/**
	 * A ViewHolderClass which contains all Views of a list item to avoid
	 * findViewById() calls.
	 * 
	 */
	public static class ViewHolderTrack implements ViewHolder<TrackListItem> {
		/**
		 * Reference to the trackListItem which is shown in the ViewHolder.
		 */
		public TrackListItem trackListItem;
		/**
		 * View which shows the name of the current track.
		 */
		public TextView trackName;
		/**
		 * View which shows the time when the track was created.
		 */
		public TextView trackAddedTime;

		@Override
		public boolean isHeader() {
			return false;
		}

		@Override
		public TrackListItem getItem() {
			return trackListItem;
		}
	}

	/**
	 * A ViewHolderClass which contains all Views of a list header to avoid
	 * findViewById() calls.
	 * 
	 */
	public static class ViewHolderHeader implements ViewHolder<TrackListItem> {
		/**
		 * View which shows the name of the current header.
		 */
		TextView headerName;

		@Override
		public boolean isHeader() {
			return true;
		}

		@Override
		public TrackListItem getItem() {
			return null;
		}
	}

	/**
	 * Contains basic information about a Track.
	 * 
	 */
	public static class TrackListItem implements ListItem {

		private boolean isHeader;
		/**
		 * If the item is a header it's name is stored in headerName.
		 */
		String headerName;
		/**
		 * The number of the item in the list.
		 */
		int nr;
		/**
		 * Contains id, name and date of a track.
		 */
		public TrackRecord trackRecord;

		/**
		 * Constructor for standard items.
		 * 
		 * @param trackRecord which belongs to the item
		 * @param nr of the item in the list
		 */
		TrackListItem(TrackRecord trackRecord, int nr) {
			this.trackRecord = trackRecord;
			this.nr = nr;
			this.isHeader = false;
		}

		/**
		 * Constructor for headers.
		 * 
		 * @param headerName name of the header.
		 */
		TrackListItem(String headerName) {
			this.isHeader = true;
			this.headerName = headerName;
		}

		@Override
		public String toString() {
			if (!isHeader)
				return trackRecord.trackName;
			return "";
		}

		@Override
		public boolean isHeader() {
			return isHeader;
		}

	}
}
